
<?php
session_start(); 
if( !$_SESSION["login"])header('Location: error_page.php');
	include("db_util.php");
	putenv("ORACLE_HOME=/usr/local/libexec/oracle/app/oracle/product/11.2.0/client_1");
	$connection=DbConnect();
 	$query = "select max(albID) as MAX from album";
	$stmt = oci_parse($connection, $query);
	oci_execute($stmt);
	$max_id = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);
	$next_id = $max_id["MAX"] + 1;
	date_default_timezone_set('EST');
	$query="Insert into album values(" . $next_id . ",'" . $_POST["AlbumName"] . "','" . "./picture/student/" .$_SESSION["StuID"] . 		      "/" . $_POST["AlbumName"] . "/" . $_FILES["file"]["name"] . "'," . $_SESSION["StuID"] .  ",'" . date('d-M-y') . "','Y','".      $_POST["Description"] . "')"; 
	 
	 
	
	$statement = oci_parse($connection, $query);
	oci_execute($statement);
	$check=false;
    if($_FILES["file"]!="") $check=UploadPic($_FILES["file"],$next_id, $connection);
   	oci_free_statement($statement);
	 
	  oci_close($connection);
    if($check==true){ 
		echo "<p>Pictures are successfully uploaded!</p>";
    	echo "<p>Return to view the picture you upload, please click <a href=AblumView.php?AID=". $next_id . ">here</a></p>";
	}
	
 function UploadPic($file, $albumID, $connection)
  {
  
	
	if ((($file["type"] == "image/gif")
	|| ($file["type"] == "image/jpeg")
	|| ($file["type"] == "image/pjpeg"))
	)
  {
  if ($file["error"] > 0)
    {
    echo "Return Code: " . $file["error"] . "<br />";
    }
  else
    {
   

   
	 
	
	 $query = "select max(PID) as MAX from photo";
	 $stmt = oci_parse($connection, $query);
	 oci_execute($stmt);
	 $max_id = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);
	 $next_id = $max_id["MAX"] + 1;
			
	 date_default_timezone_set('EST');
	 $query="Insert into photo values(" . $next_id . ",'"  . $file["name"] . "','" .  "./picture/student/" .$_SESSION["StuID"] . "/" .        $_POST["AlbumName"] . "/'," . $albumID . ",'" . date('d-M-y') . "','Y')"; 
	 $statement = oci_parse($connection, $query);
	 oci_execute($statement);
	
	
	  //mkdir   ($testdir,   0777); 
	  if (!file_exists("./picture/student/" .$_SESSION["StuID"] . "/" . $_POST["AlbumName"] . "/" )) mkdir ("./picture/student/" .        $_SESSION["StuID"] . "/" . $_POST["AlbumName"] . "/", 0777);
      move_uploaded_file($file["tmp_name"],
      "./picture/student/" .$_SESSION["StuID"] . "/" . $_POST["AlbumName"] . "/" . $file["name"]);
      return true;
      
    }
  }
  else return false;

 


}
  
?>
